package com.timoon.sample.web.role;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.timoon.sample.dao.ResourceDAO;
import com.timoon.sample.dao.RoleResourceDAO;
import com.timoon.sample.dao.model.ResourceExample;
import com.timoon.sample.dao.model.RoleResource;
import com.timoon.sample.dao.model.RoleResourceExample;

@Service
public class RoleCreatorService {

	@Resource
	private ResourceDAO resourceDAOImpl;
	
	@Resource
	private RoleResourceDAO roleResourceDAOImpl;
	
	public List<Object> getAllResource(){
		List<Object> objs=this.resourceDAOImpl.selectByExample(new ResourceExample());
		
		return objs;
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getResourceIdsByRole(final String roleId){
		
		List<String> ids=new ArrayList<String>();
		
		if(roleId!=null){
			List<RoleResource> rrs=this.roleResourceDAOImpl.selectByExample(new RoleResourceExample(){
				{
					this.createCriteria().andRoleIdEqualTo(Integer.valueOf(roleId));
				}
			});
			if(rrs!=null){
				for(RoleResource rr:rrs){
					ids.add(rr.getResourceId().toString());
				}
			}
		}
		
		return ids;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object> getResourceListByRoleId(final Integer roleId){
		List<Object> list=new ArrayList<Object>();
		
		if(roleId!=null){
			List<RoleResource> rrs=this.roleResourceDAOImpl.selectByExample(new RoleResourceExample(){
				{
					this.createCriteria().andRoleIdEqualTo(Integer.valueOf(roleId));
				}
			});
			if(rrs!=null){
				for(RoleResource rr:rrs){
					list.add(resourceDAOImpl.selectByPrimaryKey(rr.getResourceId()));
				}
			}
		}
		return list;
	}

	public ResourceDAO getResourceDAOImpl() {
		return resourceDAOImpl;
	}

	public void setResourceDAOImpl(ResourceDAO resourceDAOImpl) {
		this.resourceDAOImpl = resourceDAOImpl;
	}
	public RoleResourceDAO getRoleResourceDAOImpl() {
		return roleResourceDAOImpl;
	}
	public void setRoleResourceDAOImpl(RoleResourceDAO roleResourceDAOImpl) {
		this.roleResourceDAOImpl = roleResourceDAOImpl;
	}
}
